package com.huawei.cloud.phone.management.domain.pkg;

import com.huawei.cloud.phone.management.common.BeanFactory;
import com.huawei.cloud.phone.management.common.GlobalConfig;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Slf4j
@Data
@ToString(exclude = "inputStream")
public class ObsFileData {
    private String bucketName;

    private String objectPath;

    private InputStream inputStream;

    /**
     * 转换成OBS文件数据
     *
     * @param pkgName 应用包名称
     * @param file    应用文件
     * @return OBS文件数据
     */
    public static ObsFileData convertObsFileData(String pkgName, MultipartFile file) {
        try {
            ObsFileData apkData = new ObsFileData();
            apkData.setInputStream(file.getInputStream());
            apkData.setObjectPath("apk/" + pkgName + "/" + file.getOriginalFilename());
            apkData.setBucketName(BeanFactory.getBean(GlobalConfig.class).getBucketName());
            return apkData;
        } catch (IOException e) {
            log.error("get file content failed", e);
            throw new RuntimeException(String.format("get data from file %s failed", file.getOriginalFilename()));
        }
    }
}
